package fr.umlv.ig.listenerFactory;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

import fr.umlv.ig.adapter.StationTreeModel;
import fr.umlv.ig.metroFactory.GraphicFactory;
import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.Station;

/**
 * Class with static methods which contains listener for a {@link JTree} with a
 * {@link StationTreeModel}
 * 
 * @author jenseric
 * @author wiliam
 *
 */
public class StationTreeListenerFactory {
	
	/**
	 * Returns a {@link TreeSelectionListener} for a {@link JTree}
	 * @param jtree the {@link JTree} which listen with the listener returned.
	 * @return a {@link TreeSelectionListener} for a {@link JTree} with a 
	 * {@link StationTreeModel}.
	 */
	public static TreeSelectionListener getSelectionListener(final JTree jtree) {
		final StationTreeModel treeModel = (StationTreeModel) jtree.getModel();
		return new TreeSelectionListener() {
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				if(jtree.getLastSelectedPathComponent() != null){
					Object o = jtree.getLastSelectedPathComponent();
					
					if(o instanceof Line ) {
						treeModel.selected((Line) o);
					}
				}
			}
		};
	}
	
	/**
	 * {@link MouseAdapter} for a {@link JTree} which has a {@link StationTreeModel}.
	 * @param jtree the JTree which will have the {@link MouseAdapter}
	 * @return {@link MouseAdapter} for a {@link JTree} which has a 
	 *          {@link StationTreeModel}
	 */
	public static MouseAdapter getMouseAdapter(final JTree jtree) {
		final StationTreeModel treeModel = (StationTreeModel) jtree.getModel();
		
		return new MouseAdapter() { 
			@Override
			public void mousePressed(MouseEvent e) { 
				if (SwingUtilities.isRightMouseButton(e)) { 
					int selRow = jtree.getRowForLocation(e.getX(), e.getY()); 
					TreePath selPath = jtree.getPathForLocation(e.getX(), e.getY()); 
					if (selRow != -1) { 
						jtree.clearSelection(); 
						jtree.setSelectionPath(selPath); 
						if(jtree.getLastSelectedPathComponent() instanceof Station) {
							Station station = (Station)jtree.getLastSelectedPathComponent();
							if(treeModel.isOpened(station)) {
								treeModel.closeStation(station);
							}
							else {
								treeModel.openStation(station);
							}
						}
					} 
				} 
				else if(SwingUtilities.isLeftMouseButton(e)){
					if(jtree.getLastSelectedPathComponent() instanceof Station) {
						Station station = (Station)jtree.getLastSelectedPathComponent();
						if(treeModel.isOpened(station)) {
							treeModel.selectedStation(station);
						}
					}					
				}
			} 
		};
	}
	
	/**
	 * {@link DefaultTreeCellRenderer} for a {@link StationTreeModel}.
	 * @param treeModel the {@link StationTreeModel} concerned.
	 * @return a {@link DefaultTreeCellRenderer} for a {@link StationTreeModel}.
	 */
	@SuppressWarnings("serial")
	public static DefaultTreeCellRenderer getTreeCellRenderer(final StationTreeModel treeModel) {
		return new DefaultTreeCellRenderer() {
			@Override
			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
				if (value instanceof Line) {
					Line l = (Line)value;
					value="Line" + l.getNumber();
					setClosedIcon(GraphicFactory.ICONLINE);
					setOpenIcon(GraphicFactory.ICONLINE);

				}

				if (value instanceof Station) {
					Station station = (Station)value;
					if(treeModel.isOpened(station))
						setLeafIcon(GraphicFactory.ICONSTATION);
					else {
						setLeafIcon(GraphicFactory.ICONSTATIONSTOP);
					}
					value= station.getName();
				}

				return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
			}
		};
	}
		
	
}
